<?php
namespace app\admin\service;

use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class TmpMessageService
{
    /**
     * 获取accesstoken
     */
    public static function getAccessToken($config){
        if (Cache::get($config['AppID']."wx_access_token")){
            $res = Cache::get($config['AppID']."wx_access_token");
        }else{
            $url=$config['wxAccessTokenUrl'].'appid='.$config['AppID'].'&secret='.$config['AppSecret'].'&grant_type=client_credential';
            $rs = json_decode(curl_get_contents_temp($url), true);
            Cache::set($config['AppID']."wx_access_token",$rs['access_token'],6900);
			$res= $rs['access_token'];
        }
        return $res;
    }

    /**
     * 发送消息
     */
    public static function sendSubscribe($config,$data){
        $url=$config["wxSendSubscribeUrl"]."access_token=".self::getAccessToken($config);
        $postData = [
            "touser"=>$data["open_id"],
            "template_id"=>$data["template_id"],
            "page"=>$data["page"],
            "data"=>$data["data"]
        ];
        $rs = json_decode(https_request($url,$postData,'json'), true);
        return $rs;
    }
}